﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.PageActionManager;

namespace ZBJF.Warranty.BLL
{
    public partial class PageMenuService
    {
        public bool IsExist(byte applicationType, string pageName, int parentId, int id = 0)
        {
            var expressionWhere = GetPageMenuWhereExpression(new PageMenuModel() { BelongApplicationType = (byte)applicationType, PageMenuName = pageName, ParentID = parentId });
            if (id > 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        public PageMenu Load(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public IQueryable<PageMenu> LoadBy(ApplicationType applicationType, bool showDisabeldMenu = false)
        {
            var expressionWhere = GetPageMenuWhereExpression(new PageMenuModel() { BelongApplicationType = (byte)applicationType, ShowDisabeldMenu = showDisabeldMenu });
            return LoadEntities(expressionWhere);
        }

        public IQueryable<PageMenu> LoadBy(ApplicationType applicationType, int parentId, bool showDisabeldMenu = false)
        {
            var expressionWhere = GetPageMenuWhereExpression(new PageMenuModel() { BelongApplicationType = (byte)applicationType, ParentID = parentId, ShowDisabeldMenu = showDisabeldMenu });
            return LoadEntities(expressionWhere);
        }




        public string GetFullPageMenuName(int id)
        {
            var fullName = string.Empty;
            var firstOrDefault = CacheHelper.Instance.GetDataFromCache("AllPageMenus1", () => LoadEntities(o => true).ToList()).FirstOrDefault(o => o.ID == id);
            if (firstOrDefault != null)
            {
                var parentPageMemu = GetParentPageMenu(firstOrDefault.ParentID);
                if (parentPageMemu != null)
                    fullName = GetFullPageMenuName(firstOrDefault.ParentID) + "→" + firstOrDefault.PageName + fullName;
                else
                {
                    fullName = fullName + "→" + firstOrDefault.PageName;
                }
            }
            return fullName.Trim('→');
        }

        public PageMenu GetParentPageMenu(int parentID)
        {
            return
                CacheHelper.Instance.GetDataFromCache("AllPageMenus2", () => LoadEntities(o => true).ToList())
                    .FirstOrDefault(o => o.ID == parentID);
        }

        /// <summary>
        /// 获取所有的子菜单ID(包含本身）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<int> GetAllChildrenPageID(int id)
        {
            var pageIds = GetChildrenPageID(id).Select(o => o.ID).ToList();
            pageIds.Add(id);
            return pageIds;
        }
        /// <summary>
        /// 递归 todo 会影响性能吗？
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private IEnumerable<PageMenu> GetChildrenPageID(int id)
        {
            var query = from c in LoadEntities(o => true)
                        where c.ParentID == id
                        select c;

            return query.ToList().Concat(query.ToList().SelectMany(t => GetChildrenPageID(t.ID)));
        }
        public IQueryable<PageMenu> LoadAllPageMenus()
        {
            var expressionWhere = GetPageMenuWhereExpression(new PageMenuModel());
            return LoadEntities(expressionWhere);
        }
        private Expression<Func<PageMenu, bool>> GetPageMenuWhereExpression(PageMenuModel model)
        {
            var expressionWhere = PredicateExtensions.True<PageMenu>();

            if (model.ID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID == model.ID);
            }
            if (model.ParentID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ParentID == model.ParentID);
            }
            if (model.BelongApplicationType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.BelongApplicationType == model.BelongApplicationType);
            }
            if (!string.IsNullOrEmpty(model.PageMenuName))
            {
                expressionWhere = expressionWhere.And(o => o.PageName.Equals(model.PageMenuName));
            }
            if (model.ShowDisabeldMenu.HasValue)
            {
                expressionWhere = expressionWhere.And(o => model.ShowDisabeldMenu.Value || o.IsShow);
            }
            return expressionWhere;
        }
    }
}
